<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/27
 * Time: 09:38
 */

namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\HttpResponse;
use Helpers\User;
use Helpers\XhException;

class ResAreaGroup
{
    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    /**
     * 初始化配置
     *
     * @throws \Exception
     *
     * create by qgz 20180727
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config    = Main::getConfigByName('resServerInterface');//接口资源中心
            self::$url = Tool::getValue($config, 'questionPrivilegeUrl'); //资源权限接口
            if (!self::$url) {
                throw new \Exception('资源权限接口url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 获取区域组数据列表
     *
     * @param $page
     * @param $size
     * @param $stage
     * @param $area
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180727
     */
    public static function getAreaGroupList($page, $size, $stage, $area)
    {
        $params = [
            'page' => $page,
            'size' => $size
        ];
        if ($stage) {
            $params['stageType'] = $stage;
        }
        if ($area) {
            foreach ($area as $key => $value) {
                $params[$key] = $value;
            }
        }
        self::init();
        $response = self::$httpClient->get('/api/v1/groupInfo/match', $params);
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return $body;
    }

    /**
     * 获取区域组数据列表
     *
     * @param $params
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180727
     */
    public static function getAreaGroupData($params)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/groupInfo/match', $params);
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return $body;
    }

    /**
     * 删除区域组
     *
     * @param $id
     *
     * @return mixed|string
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180729
     */
    public static function delGroupArea($id)
    {
        self::init();
        $response = self::$httpClient->delete(sprintf('/api/v1/groupInfo/%d', $id), ['userId' => User::getId()]);
        return self::getResult($response);
    }

    /**
     * 根据学段、省查询已设置区域信息
     *
     * @param $stage
     * @param $province
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180802
     */
    public static function getAreaByStagePro($stage, $province)
    {
        $params = [
            'stageType'  => $stage,
            'provinceId' => $province
        ];
        self::init();
        $response = self::$httpClient->get('/api/v1/groupDistricts/stageType', $params);
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return $body;
    }

    /**
     * 根据学校id查询区域教材信息，即黑白正常名单
     *
     * @param $schoolId
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180802
     */
    public static function getSchoolTextBook($schoolId)
    {
        $params = [
            'schoolId' => $schoolId
        ];
        self::init();
        $response = self::$httpClient->get('/api/v1/groupTextBooks/district', $params);
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return $body;
    }

    /**
     * 根据组id获取组基本信息
     *
     * @param $groupId
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180802
     */
    public static function getGroupInfoById($groupId)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/groupInfo/' . $groupId);
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return $body;
    }

    /**
     * 根据组id获取教材版本数据
     *
     * @param $groupId
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180802
     */
    public static function getSubVerById($groupId)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/groupTextBooks/' . $groupId);
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return $body;
    }

    /**
     * 添加区域组信息
     *
     * @param $params
     * @param string $groupId
     *
     * @return array
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180730
     */
    public static function saveGroupInfo($params, $groupId = '')
    {
        self::init();
        $url      = $groupId ? '/api/v1/groupInfo/' . $groupId : '/api/v1/groupInfo';
        $response = self::$httpClient->postJson($url, $params);
        $body     = $response->toArray();
        if (isset($body['code']) && $body['code'] == 400) {
            return ['type' => 1, 'msg' => $body['msg']];
        }
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201 && $body['code'] != 400) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return ['type' => 2, 'groupId' => $body['groupId']];
    }

    /**
     * 保存版本信息
     *
     * @param $params
     *
     * @return mixed|string
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180730
     */
    public static function saveSubjectVersion($params)
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/groupTextBooks', $params);
        return self::getResult($response);
    }

    /**
     * 保存地域信息
     *
     * @param $params
     *
     * @return mixed|string
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180807
     */
    public static function saveAreaData($params)
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/groupDistricts', $params);
        return self::getResult($response);
    }

    /**
     * 保存白名单
     *
     * @param $params
     *
     * @return mixed|string
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180809
     */
    public static function saveWhiteData($params)
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/textBookWhites', $params);
        return self::getResult($response);
    }

    /**
     * 保存黑名单
     *
     * @param $params
     *
     * @return mixed|string
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180809
     */
    public static function saveBlackData($params)
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/textBookBlacks', $params);
        return self::getResult($response);
    }

    /**
     * 根据组id获取地区数据
     *
     * @param $groupId
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     * @throws \Exception
     *
     * create by qgz 20180807
     */
    public static function getAreaDataById($groupId)
    {
        self::init();
        $response = self::$httpClient->get('/api/v1/groupDistricts/' . $groupId);
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return $body;
    }


    /**
     * 检查错误并获取结果
     *
     * @param HttpResponse $response
     * @param string $result
     * @param bool $default
     *
     * @return mixed|string
     * @throws XhException
     *
     * create by qgz 20180727
     */
    public static function getResult(HttpResponse $response, $result = 'results', $default = true)
    {
        if (!$response->isSuccess()) {
            $body = $response->toArray();
            throw new XhException('接口错误：' . $body['msg'], $body['msg']);
        }
        $body = $response->toArray();
        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201) {
            throw new XhException(Tool::getValue($body, 'msg', '请求失败'), $response);
        }
        return Tool::getValue($body, $result, $default);
    }
}
